<script type="text/javascript">
	function showReplyDiv(val) {		
		if(document.getElementById("replyDiv["+val+"]").style.display=="none") {
			document.getElementById("replyDiv["+val+"]").style.display="block";
		}
		else { 
			document.getElementById("replyDiv["+val+"]").style.display="none";
		}
	}
	
	function funSellerReply(bargain,replyTo) {		
		frm = document.frmProductOffer;
		document.getElementById("hidSellerReply").value=bargain;
		document.getElementById("hidReplyFor").value=replyTo;
		document.getElementById("hidType").value="add";
		frm.submit();		
	}
	
	function funDeleteBargain(bargain) {
		frm = document.frmProductOffer;
		document.getElementById("hidSellerReply").value=bargain;
		document.getElementById("hidType").value="delete";
		frm.submit();		
	}
	
</script>
<style type="text/css">
	div.reply { padding:5px; border:1px solid #CCC; }
	div.reply label { float:left; width:100px; }
</style>
<?php
if(!empty($_REQUEST['SellProduct']))
  $itemId=$_REQUEST['SellProduct'];
else
  $itemId=$_REQUEST['products'];
   $sqlProOffers="SELECT * FROM tbl_bargain where item_id='$itemId' and status='offer' and bargain_type=1";
	$rstProOffers=mysql_query($sqlProOffers);
	$noProOffers=mysql_num_rows($rstProOffers);		
		if($noProOffers > 0) { 
	while($rowProOffers = mysql_fetch_array($rstProOffers))
				$pro_offers[]=$rowProOffers; 
	}
	if(is_array($pro_offers) > 0) { ?>
		<table width='100%' border='0' align='center' cellspacing='0' cellpadding='0'>	
		<?php $i=0;
		foreach($pro_offers as $offer) { ?>
			<tr>
			  <td>&nbsp;</td>
		    <td height="30"><strong>Offer</strong></td><td><strong>Price</strong></td></tr>
			<tr>
			    <td width='3%'>&nbsp;</td>
				<td width='87%'><?php echo $offer['comment']; ?></td>
				<td width='10%'>$<?php echo number_format($offer['offer_amount'],2); ?></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td colspan='2'>
				 
				<?php 
				$sqlSelparent="SELECT * FROM tbl_bargain where parent_id='".$offer['bargain_id']."' and item_id='$itemId' and bargain_type=1";
		       $rstSelparent=mysql_query($sqlSelparent);
			   $pro_reply= mysql_fetch_array($rstSelparent);						
				if($pro_reply=="") {
					if($userId==$productOwner) {
						?>								
						<a href='javascript:void(0);' onClick="showReplyDiv('<?php echo $i; ?>');">Reply</a>
						<br>
						<div class="reply" id="replyDiv[<?php echo $i; ?>]" style="display:none;">
							<label for="askPrice">Asking price: </label> <input type="text" name="ask_amount[<?php echo $i; ?>]" size="7" value="<?php echo $offer['asking_amount']; ?>">
							<br>
							<input type="hidden" name="buyer_amount[<?php echo $i; ?>]" value="<?php echo $offer['offer_amount']; ?>">
							<label for="seller_comment">Comment: </label> 
							<textarea name="seller_comment[<?php echo $i; ?>]"></textarea><br>
							<input type="button" value="submit" onClick="funSellerReply('<?php echo $offer['bargain_id']; ?>','<?php echo $i; ?>');">
						</div>
				<?php 
					}
				} 
				
				?>
			</td></tr>
			<?php if($pro_reply!="") { ?>
			<tr bgcolor="#CCCCCC">
			  <td>&nbsp;</td>
		    <td height="30"><strong>Seller's Response</strong></td><td><strong>Price</strong></td></tr>
			<tr bgcolor="#CCCCCC">
			  <td width='3%'>&nbsp;</td>
				<td width='87%'><?php echo $pro_reply['comment'];
				if($userId==$productOwner) {
				?>	<br>							
					<a href='javascript:void(0);' onClick="funDeleteBargain('<?php echo $pro_reply['bargain_id']; ?>');">Delete</a>
						
				<?php } ?>
				</td>
				<td width='10%'>$<?php echo number_format($pro_reply['asking_amount'],2); ?>
				
				</td>
			</tr>
			<tr bgcolor="#CCCCCC">
			  <td>&nbsp;</td>
			  <td>&nbsp;</td>
			  <td>&nbsp;</td>
		  </tr>	
			<?php } 
		$i++; 
	      } ?> 
		</table>
		<?php
	}
	else { echo "<br>No Offers found.";	 }

?>
